<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     default_default
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */
?>
<?php /* @var $this Enterprise_GiftCard_Block_Adminhtml_Catalog_Product_Composite_Fieldset_Giftcard */?>
<?php $_product = $this->getProduct(); ?>
<?php $_id = $_product->getId(); ?>
<?php $_storeId = $_product->getStoreId(); ?>
<?php $_skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck(); ?>
<script>
//<![CDATA[
productConfigure.giftcardConfig = {};
//]]>
</script>
<div id="catalog_product_composite_configure_fields_downloadable" class="giftcard-form <?php echo $this->getIsLastFieldset() ? 'last-fieldset' : '' ?>">

<?php if (!$_product->isAvailable() && !$_skipSaleableCheck): ?>
    <p class="availability out-of-stock"><?php echo $this->helper('catalog')->__('Availability:') ?> <span><?php echo $this->helper('catalog')->__('Out of stock') ?></span></p>
<?php endif; ?>

    <div class="product-options">
<?php if (($_product->isSaleable() || $_skipSaleableCheck) && $this->isConfigured($_product)): ?>
    <?php if (!$this->isOpenAmountAvailable($_product) && (count($this->getAmounts($_product)) == 1)): ?>
        <?php $_amount = $this->getAmounts($_product); $_amount = array_shift($_amount); ?>
        <label><?php echo Mage::helper('enterprise_giftcard')->__('Amount'); ?></label>
        <div class="input-box">
            <span class="regular-price">
                <span class="price" id="product-price-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>">
                    <input type="hidden" value="<?php echo $_amount ?>" price="<?php echo $_amount ?>" name="giftcard_amount" />
                    <?php echo Mage::helper('core')->currencyByStore($_amount, $_storeId, true, false); ?>
                </span>
            </span>
        </div>

    <?php else: ?>
        <div class="giftcard-amount-form">
            <ul class="form-list">
                <li class="fields">
                    <div class="field">
                        <?php if ($this->isAmountAvailable($_product)): ?>
                            <label for="giftcard_amount" id="amount_label_select" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Amount'); ?></label>
                        <?php
                            $configValue = $this->getDefaultValue('giftcard_amount');
                        ?>
                        <div class="input-box">
                            <select name="giftcard_amount" id="giftcard_amount" class="required-entry giftcard-amount-entry" onchange="productConfigure.giftcardConfig.switchGiftCardInputs.call(this,event||window.event)">
                                <option value=""><?php echo Mage::helper('enterprise_giftcard')->__('Choose an Amount...') ?></option>
                                <?php foreach ($this->getAmounts($_product) as $value): ?>
                                    <option value="<?php echo $value; ?>" price="<?php echo Mage::helper('core')->currencyByStore($value, $_storeId, false) ?>" <?php echo (($configValue == $value) ? 'selected' : '')?>><?php echo Mage::helper('core')->currencyByStore($value, $_storeId, true, false); ?></option>
                                <?php endforeach; ?>

                                <?php if ($this->isOpenAmountAvailable($_product)): ?>
                                    <option value="custom" <?php echo (($configValue == 'custom') ? 'selected' : '')?>><?php echo Mage::helper('enterprise_giftcard')->__('Other Amount...') ?></option>
                                <?php endif; ?>
                            </select>
                        </div>
                        <?php endif; ?>
                    </div>

                    <?php if ($this->isOpenAmountAvailable($_product)): ?>
                    <div class="field" id="giftcard_amount_input_fields">
                        <label for="giftcard_amount_input" id="amount_label_input" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Amount in %s', Mage::app()->getStore($_storeId)->getCurrentCurrencyCode()); ?></label>
                        <div id="giftcard_amount_box" class="input-box">
                            <span class="v-fix"><input type="text" id="giftcard_amount_input" name="custom_giftcard_amount" class="validate-number required-entry input-text giftcard-amount-entry giftcard-min-amount giftcard-max-amount" value="<?php echo $this->escapeHtml($this->getDefaultValue('custom_giftcard_amount')) ?>" price="<?php echo $this->escapeHtml($this->getDefaultValue('custom_giftcard_amount')) ?>" onblur="this.setAttribute('price', this.value);" /></span>

                            <?php if ($_product->getOpenAmountMin() || $_product->getOpenAmountMax()): ?>
                                <?php $_min = Mage::helper('core')->currencyByStore($_product->getOpenAmountMin(), $_storeId, true, false); ?>
                                <?php $_max = Mage::helper('core')->currencyByStore($_product->getOpenAmountMax(), $_storeId, true, false); ?>
                                <p class="notice">
                                    <?php if ($_product->getOpenAmountMax() && $_product->getOpenAmountMin()): ?>
                                        <span><?php echo Mage::helper('enterprise_giftcard')->__('Minimum: %s', $_min); ?></span>
                                        <span><?php echo Mage::helper('enterprise_giftcard')->__('Maximum: %s', $_max); ?></span>
                                    <?php elseif ($_product->getOpenAmountMin()): ?>
                                        <span><?php echo Mage::helper('enterprise_giftcard')->__('Minimum: %s', $_min); ?></span>
                                    <?php elseif ($_product->getOpenAmountMax()): ?>
                                        <span><?php echo Mage::helper('enterprise_giftcard')->__('Maximum: %s', $_max); ?></span>
                                    <?php endif; ?>
                                </p>
                                <script type="text/javascript">
                                //<![CDATA[
                                    productConfigure.giftcardConfig.minAllowedAmount = <?php echo Mage::helper('core')->currencyByStore($_product->getOpenAmountMin(), $_storeId, false, false) ?>;
                                    productConfigure.giftcardConfig.maxAllowedAmount = <?php echo Mage::helper('core')->currencyByStore($_product->getOpenAmountMax(), $_storeId, false, false) ?>;

                                    productConfigure.giftcardConfig.parsePrice = function (value) {
                                        value = value.replace('\'', '').replace(' ', '');
                                        separatorComa = value.indexOf(',');
                                        separatorDot  = value.indexOf('.');

                                        if (separatorComa != -1 && separatorDot != -1) {
                                            if (separatorComa > separatorDot) {
                                                value = value.replace('.', '').replace(',', '.');
                                            } else {
                                                value = value.replace(',', '');
                                            }
                                        } else if (separatorComa != -1) {
                                            value = value.replace(',', '.');
                                        }

                                        return parseFloat(value);
                                    }

                                    Validation.add('giftcard-min-amount', '<?php echo Mage::helper('core')->jsQuoteEscape(Mage::helper('enterprise_giftcard')->__('Entered amount is too low')); ?>', function(v) {
                                        return (productConfigure.giftcardConfig.parsePrice(v) >= productConfigure.giftcardConfig.minAllowedAmount);
                                    });
                                    Validation.add('giftcard-max-amount', '<?php echo Mage::helper('core')->jsQuoteEscape(Mage::helper('enterprise_giftcard')->__('Entered amount is too high')); ?>', function(v) {
                                        if (productConfigure.giftcardConfig.maxAllowedAmount == 0) {
                                            return true;
                                        }
                                        return (productConfigure.giftcardConfig.parsePrice(v) <= productConfigure.giftcardConfig.maxAllowedAmount);
                                    });
                                //]]>
                                </script>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                </li>
            </ul>
        </div>
        <script type="text/javascript">
        //<![CDATA[
        productConfigure.giftcardConfig.switchGiftCardInputs = function ()
        {
            if (!$('giftcard_amount') || !$('giftcard_amount_input_fields')) {
                return;
            }

            var customAmount = $('giftcard_amount_input');
            var value = $('giftcard_amount').options[$('giftcard_amount').selectedIndex].value;
            if (value == 'custom') {
                $('giftcard_amount_input_fields').show();
                if (customAmount) {
                    customAmount.disabled = false;
                }
            } else {
                $('giftcard_amount_input_fields').hide();
                if (customAmount) {
                    customAmount.disabled = true;
                }
            }
        }

        if ($('giftcard_amount') && $('giftcard_amount_input_fields')) {
            $('giftcard_amount_input_fields').hide();
        }

        productConfigure.giftcardConfig.switchGiftCardInputs();
        //]]>
        </script>
    <?php endif; ?>

        <?php echo $this->getChildHtml('product.composite.giftcard.qty') ?>

        <div class="giftcard-send-form">
            <ul class="form-list">
                <li class="fields">
                    <div class="field">
                        <label for="giftcard_sender_name" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Sender Name'); ?></label>
                        <div class="input-box">
                            <input type="text" id="giftcard_sender_name" name="giftcard_sender_name" class="required-entry input-text" value="<?php echo $this->escapeHtml($this->getDefaultValue('giftcard_sender_name')) ?>" />
                        </div>
                    </div>
                    <?php if ($this->isEmailAvailable($_product)): ?>
                    <div class="field">
                        <label for="giftcard_sender_email" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Sender Email'); ?></label>
                        <div class="input-box">
                            <input type="text" id="giftcard_sender_email" name="giftcard_sender_email" class="validate-email required-entry input-text" value="<?php echo $this->escapeHtml($this->getDefaultValue('giftcard_sender_email')) ?>"/>
                        </div>
                    </div>
                    <?php endif; ?>
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="giftcard_recipient_name" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Recipient Name'); ?></label>
                        <div class="input-box">
                            <input type="text" id="giftcard_recipient_name" name="giftcard_recipient_name" class="required-entry input-text" value="<?php echo $this->escapeHtml($this->getDefaultValue('giftcard_recipient_name')) ?>" />
                        </div>
                    </div>
                    <?php if ($this->isEmailAvailable($_product)): ?>
                    <div class="field">
                        <label for="giftcard_recipient_email" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Recipient Email'); ?></label>
                        <div class="input-box">
                            <input type="text" id="giftcard_recipient_email" name="giftcard_recipient_email" class="validate-email required-entry input-text" value="<?php echo $this->escapeHtml($this->getDefaultValue('giftcard_recipient_email')) ?>" />
                        </div>
                    </div>
                    <?php endif; ?>
                </li>
            <?php if ($this->isMessageAvailable($_product)): ?>
                <li>
                    <div class="field">
                        <label for="giftcard_message"><?php echo Mage::helper('enterprise_giftcard')->__('Message'); ?></label>
                        <div class="input-box">
                            <textarea name="giftcard_message" id="giftcard_message" class="input-text giftcard-message-max-length" cols="5" rows="3"><?php echo $this->escapeHtml($this->getDefaultValue('giftcard_message')) ?></textarea>
                            <?php if ($this->getMessageMaxLength()): ?>
                                <script type="text/javascript">
                                    Validation.add('giftcard-message-max-length', '<?php echo Mage::helper('core')->jsQuoteEscape(Mage::helper('enterprise_giftcard')->__('Maximum length of the message is %d characters.', $this->getMessageMaxLength())); ?>', function(v) {
                                        return v.length <= <?php echo $this->getMessageMaxLength(); ?>;
                                    });
                                </script>
                            <?php endif; ?>
                        </div>
                    </div>
                </li>
            <?php endif; ?>
            </ul>
        </div>
    <?php endif; ?>
    </div>
</div>
